﻿/*  ----------------------------------------------------------------------------
 *  Microsoft Public License (http://www.microsoft.com/opensource/licenses.mspx#Ms-PL)
 *  
 *  ----------------------------------------------------------------------------
 *  DynamicAspect
 *  ----------------------------------------------------------------------------
 *  File:       AspectBase.cs
 *  Author:     NiceWk (n9walker@hotmail.com)
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.ComponentModel.Composition;

namespace Bizall.Dynamic.Aspect
{
    /// <summary>
    /// An abstract base class for all aspect objects.
    /// </summary>
    [InheritedExport(typeof(IAspect))]
    public abstract class AspectBase : IAspect
    {

        #region IAspect Members
        /// <summary>
        /// Providers implementation for IAspect Targets property <see cref="P:IAspect.Targets"/>
        /// </summary>
        public List<string> Targets { get; private set;}

        /// <summary>
        /// Constructor of AspectBase
        /// </summary>
        public AspectBase()
        {
            Targets = new List<string>();
            Sequence = -1;
            Enabled = true;
        }

        /// <summary>
        /// Providers implementation for IAspect IsMatch method. <see cref="M:IAspect.IsMatch(string)"/>
        /// </summary>
        /// <param name="target">The name of target class.</param>
        /// <returns>Return true if there is any pattern match the name of target class.</returns>
        public virtual bool IsMatch(string target)
        {
            if (Targets.Count == 0)
                return true;

            return Targets.Any(t => Regex.IsMatch(target, t));
        }

        /// <summary>
        /// Default implmentation of <see cref="IAspect"/>'s OnBeforeMethodCall method.
        /// </summary>
        /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param>
        public virtual void OnBeforeMethodCall(WeavingContext context) { }

        /// <summary>
        /// Default implmentation of <see cref="IAspect"/>'s OnHasArroundMethod method.
        /// </summary>
        /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param>
        public virtual bool HasArroundMethod(WeavingContext context)
        {
            return false;
        }

        /// <summary>
        /// Default implmentation of <see cref="IAspect"/>'s OnAroundMethodCall method.
        /// </summary>
        /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param>
        public virtual void OnAroundMethodCall(WeavingContext context) { }

        /// <summary>
        /// Default implmentation of <see cref="IAspect"/>'s OnAfterMethodCall method.
        /// </summary>
        /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param>
        public virtual void OnAfterMethodCall(WeavingContext context) { }

        /// <summary>
        /// Default implmentation of <see cref="IAspect"/>'s OnBeforeMethodCall method.
        /// </summary>
        /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param>
        public virtual bool OnExceptionMethodCall(WeavingContext context, Exception ex)
        {
            return false;
        }

        /// <summary>
        /// Default implmentation of <see cref="IAspect"/>'s OnPropertyChanging method.
        /// </summary>
        /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param>
        public virtual void OnPropertyChanging(WeavingContext context) { }
        /// <summary>
        /// Defualt implmentation of <see cref="IAspect"/>'s OnPropertyChanged method.
        /// </summary>
        /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param>
        public virtual void OnPropertyChanged(WeavingContext context) { }

        /// <summary>
        /// Implmentation of <see cref="IAspect"/>'s OnBeforeGetValue method.
        /// </summary>
        /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param>
        public virtual void OnBeforeGetValue(WeavingContext context) { }

        /// <summary>
        /// Implmentation of <see cref="IAspect"/>'s OnAfterGetValue method.
        /// </summary>
        /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param>
        public virtual void OnAfterGetValue(WeavingContext context) { }

     /// <summary>
     /// Provides implementation for <see cref="IAspect"/> interface. The value will take effect for the method be invoked order when more than one advices apply.
     /// </summary>
        public int Sequence {get; set;}
     
        /// <summary>
        /// Provides implementation for <see cref="IAspect"/> interface. Default value is true, set to false if you do not weave this aspect object.
         /// </summary>
        public bool Enabled {get; set;}
      

        #endregion
    }
}